package com.we823.utils.web;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	public static String getDateString() {
		return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
	}

	public static Calendar parse(Date datetime) {
		if (datetime == null) {
			datetime = new Date();
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(datetime);
		return calendar;
	}

	public static Integer getYear() {
		return Calendar.getInstance().get(Calendar.YEAR);
	}

	public static Integer getMonth() {
		return Calendar.getInstance().get(Calendar.MONTH);
	}

	public static Integer getDay() {
		return Calendar.getInstance().get(Calendar.DATE);
	}

	public static Integer getMaxDay(int year, int month) {
		if (year > 0 && month > 0) {
			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.YEAR, year);
			calendar.set(Calendar.MONTH, month);
			return calendar.getMaximum(Calendar.DATE);
		}
		return 31;
	}

	public static String parse(Date date, String format) {
		if(date!=null) {
			SimpleDateFormat dateFormat = new SimpleDateFormat(format);
			return dateFormat.format(date);
		}
		return "";
	}
}
